<!--
/**************************************************/
/* Programmer: Scott Gildersleeve                 */
/*       Date: 2/14/2013                          */
/*  Page Name: GradeBook.php                      */
/*                                                */
/**************************************************/

/**************************************************/
/* Grade book for teachers.                       */
/*                                                */
/**************************************************/

/**************************************************/
/*   Date        Reviser       Revision           */
/* --------     ---------     ----------          */
/* 3/9/2013     S. Gilder     Allow Datatables    */
/*                       	  to be editable      */ 
/* 3/21/2013    S. Gilder     Added statisitcs    */
/*							                      */
/*                                                */
/**************************************************/
-->

<?php

require_once('session_handler.php');
session_handler_start();

?>
<!-- Stylesheets for overall look -->
<link rel="stylesheet" type="text/css" href="css/template.css" />
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.10.0.custom.css" />
<link rel="stylesheet" type="text/css" href="css/gradebook.css" />

<!-- Stylesheets for datatables -->
<link rel="stylesheet" type="text/css" href="DataTables/media/css/demo_table_jui.css" />
<link rel="stylesheet" type="text/css" href="DataTables/media/css/TableTools_JUI.css" />

<!-- jQuery javascript files -->
<script type="text/javascript" src="JQuery/jquery-1.9.0.min.js"></script>  
<script type="text/javascript" src="JQuery/jquery-ui-1.10.0.custom.js"></script>

<script type="text/javascript" src="JS/GradeBook.js"></script>


<!-- Datatables and Datatables plugins jQuery files -->
<script type="text/javascript" charset="utf-8" src="DataTables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="DataTables/media/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8" src="DataTables/media/js/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="DataTables/examples/examples_support/jquery.jeditable.js"></script>

<!-- jQuery Functions -->
<script type="text/javascript" charset="utf-8">	
 $(document).ready( function () {
	/* jQuery-UI Tabs function */		
	$("#tabs").tabs();
		
	/* Initialize Datatables GradeBook */
	var Tables = $("table[id^='table_id']").dataTable({
	    "bJQueryUI"      : true,
		"sPaginationType": "full_numbers",
	    "sDom"           : '<"H"Tfr>t<"F"ip>',

	    // /* Server Side Editable Processing Example */
		// "fnDrawCallback" : function () {
			// $("table[id^='table_id'] tbody td:not(.noneditable):not(.noneditableavg)").editable('datatables_postback.php', {
			// "callback": function( sValue, y ) {
			    //Update the database with the information given
				// $.ajax({
					// url: "webservices.php",
					// type: "POST",
					// data: { Func: 'Update_test_rec', TestRecID: $(this)[0].id, TestGrade: sValue },
					// dataType: 'json',
					// success: function (data) {/*console.log('success');*/},
					// error:   function (e)    {/*console.log(e);*/}
				// });
				//Redraw the table from the new data on the server
				// oTable.fnDraw();
			// },
			// "height": "20px"
			// });
		// }
    }).width("auto");
	
	/* Initialize Datatables Averages */
	$("table[id^='avg_id']").dataTable({
		"bJQueryUI"      : true,
		"sDom"           : '<t>'
	}).width("auto");
});		
</script>

<?php
require_once('webservices.php');
require_once('functions.php');


function gradebook_main()
{
 	

	$classesArray = array();

	$user_id      = $_SESSION['userid'];
	$classesArray = Get_class_info($user_id);
	$ctr 	= 0;
	$colCtr = 0;
	$avgCtr = 0;

	echo '<div id="tabs">';
	echo '<ul>';
	// Loop Controling Tab Creation
	foreach($classesArray as $key=>$value)
	{
		$ctr++;
		foreach($value as $key2=>$value2)
		{
			if($key2 == 'full_name')
			{
				echo '<li><a href="#table'.$ctr.'">'.$value2.'</a></li>';
			}
		}
	}
	echo '</ul>';
	
	$ctr = 0;
	echo '<h3 id="myButton" style="color:red;padding-left:20px">Options</h3>';
	// Loop Controling Gradebook per Class
	foreach($classesArray as $key=>$value)
	{
		$ctr++;
		
		
		// if($key == 1)
		// {	
			// echo 'value 2nd loop';
			// echo $value['course_id'];
			// print_r($value);
			// exit();
		// }
		
		$studentsArray = Get_course_book($value['course_id']);
		
		
		
		//print_r($studentsArray);
		
		$averagesArray = Get_class_stats($value['course_id']);
		$Columns = array();
		
		
		
		
		// Create the gradebook table
		echo '<div id="table'.$ctr.'">';
		echo '<table id="table_id" class="display table_class">';
	    if(count($studentsArray) != 0)
		{
			foreach($studentsArray[0] as $Column=>$value2)
			{		
				array_push($Columns, $Column);
			}
			
			echo '<thead>';
			   echo '<tr>';
			foreach($Columns as $Col)
			{


				if($Col != 'First Name' && $Col != 'Last Name' && $Col != 'Average (%)')
				{
					echo '<th class="column'.$colCtr. ' ' . str_replace(' ', '', $Col) . ' gradecolumn">'.$Col.'</th>';
				}
				else
				{
					echo '<th class="column'.$colCtr. ' ' . str_replace(' ', '', $Col) . '">'.$Col.'</th>';
				}
				
				$colCtr++;
			}

				echo '<th class="ExtraColumn"></th>';
				
				echo '</tr>';
			echo '</thead>';
			echo '<tbody>';
			
			$colCtr = 0;
			foreach ($studentsArray as $attribute=>$values)
			{
				echo '<tr>';
					foreach($values as $valindex=>$value3)
					{
						if($valindex != 'First Name' && $valindex != 'Last Name' && $valindex != 'Average (%)')
						{

							if(empty($value3))
							{
								$testgrade = array(
									0 => "nograde",
									1 => "-",
								);
					
							}
							else
							{
								$testgrade = explode(' ' , $value3);
								
							}
							echo '<td class="Column'.$colCtr.' gradecolumn testrecitem" id="' . $testgrade[0] . '">' . @$testgrade[1] . '</td>';
							
						}
						else
						{
							if($valindex == 'Average (%)')
							{
								echo '<td class="noneditableavg">';
								printf("%.2f", $value3);
								echo '</td>';
							}
							else
								echo '<td class="noneditable">' . $value3 . '</td>';
						}
					}
					
					echo '<td class="ExtraColumn noneditable"></td>';
				echo '</tr>';
				$colCtr++;
			}
			
			echo '</tbody>';
			}
		else
		{
		   echo '<thead>';
		      echo '<th></th>';
		   echo '<thead>';
		   echo '<tbody>';
		   echo '</tbody>';
		}
		echo '</table>';
		
		// Create the averages table
		echo '<br /><br />';
		echo '<h3>Averages for '. $value['full_name'] .' </h3>';
		
		$InnerColumns = array(); // Reinitialize to prevent table headers from containing the data from the table above
		
		echo '<div id="tableavg'.$avgCtr.'">';
		echo '<table id="avg_id" class="display table_class">';
		foreach($averagesArray as $InnerKey=>$InnerValue)
		{
			foreach($averagesArray[0] as $InnerColumn=>$InnerValue2)
			{		
				array_push($InnerColumns, $InnerColumn);
			}
			echo '<thead>';
			   echo '<tr>';
			foreach($InnerColumns as $InnerCol)
			{
				echo '<th class="column'.$colCtr.'">'.$InnerCol.'</th>';
				$colCtr++;
			}
				
				echo '</tr>';
			echo '</thead>';
			echo '<tbody>';
			echo '<tr>';
			foreach($InnerValue as $InnerKey2=>$InnerValue3)
			{
				if ($InnerKey2 == 'Error')
				{
					echo '<td>'.$InnerValue3.'</td>';
				}
				else
				{
					echo '<td class="noneditableavg">';
					printf("%.2f", $InnerValue3);
					echo '</td>';
				}
			}
			echo '</tr>';
			echo '</tbody>';
		}
		echo '</table>';
		echo '</div>';
			
		echo '</div>';
		$avgCtr++;
		$colCtr = 0;
	}	
	echo '</div>';
}

if (!debug_backtrace())
{
	gradebook_main();
}
?>



<div id="TestRecordDialog" class="dialog" title="Test Record Info" style="display:none;">
	<div id="TestRecDetail">
		<div class="LineDetail"><span class="label">Test Name</span><span class="value" id="TestNameVal"></span></div>
		<div class="LineDetail"><span class="label">Student Name</span><span class="value" id="StudentNameVal"></span></div>
		<div class="LineDetail"><span class="label">Date and Time</span><span class="value" id="DatetimeVal"></span></div>
		<div class="LineDetail"><span class="label">Total Points</span>
		<select id="PointsGiven"></select>
		<span id="PointOf"> / </span>
		<span id="MaxPoints"></span></div>
		<div class="LineDetail" id="GradeLine"><span class="label">Grade Percenatage</span><span class="value" id="GradeVal"></span></div>
	</div>
	<img src="images/loader.gif" style="display:none;" id="LoaderBar" />
</div>